package com.authService.service.impl;

import com.authService.convert.PermissionConvert;
import com.authService.entity.SysPermissionEntity;
import com.authService.enums.OperationLogTypeEnum;
import com.authService.mapper.SysPermissionMapper;
import com.authService.pojo.req.PermissionEditReqDTO;
import com.authService.pojo.req.PermissionPageReqDTO;
import com.authService.pojo.res.PermissionDetailResDTO;
import com.authService.pojo.res.PermissionPageResDTO;
import com.authService.service.ISysPermissionService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.operationLog.pojo.bo.OperationLogBO;
import com.common.operationLog.service.IOperationLogService;
import com.xiaopeng.common.page.PageUtil;
import com.xiaopeng.common.page.PageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 系统权限表 服务实现类
 * </p>
 *
 * @author xiaopeng
 * @since 2024-09-22
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermissionEntity> implements ISysPermissionService {


    @Resource
    private IOperationLogService operationLogService;

    @Override
    public PageVO<PermissionPageResDTO> selectPage(PermissionPageReqDTO reqDTO) {
        return PageUtil.convertPages(reqDTO, baseMapper::selectPermissionList);
    }

    @Override
    public PermissionDetailResDTO detail(Long permissionId) {
        SysPermissionEntity sysPermissionEntity = this.getById(permissionId);
        return PermissionConvert.INSTANCE.detailConvert(sysPermissionEntity);
    }

    @Override
    public void savePermission(PermissionEditReqDTO reqDTO) {
        SysPermissionEntity sysPermissionEntity = PermissionConvert.INSTANCE.editReqDTOConvert(reqDTO);
        long permissionId = IdWorker.getId();
        sysPermissionEntity.setId(permissionId);
//        sysPermissionEntity.setCreateBy(UserHolder.getUserId());
//        sysPermissionEntity.setCreateByName(UserHolder.getUserName());
//        sysPermissionEntity.setCreateTime(LocalDateTime.now());
//        sysPermissionEntity.setUpdateBy(UserHolder.getUserId());
//        sysPermissionEntity.setUpdateByName(UserHolder.getUserName());
        sysPermissionEntity.setUpdateTime(LocalDateTime.now());
        this.save(sysPermissionEntity);

        // 保存操作日志
        PermissionDetailResDTO permissionDetailResDTO = this.detail(permissionId);
        operationLogService.save(OperationLogBO.getInstance()
                .biz(OperationLogTypeEnum.PERMISSION_SAVE, permissionId, null)
                .content(reqDTO, null, permissionDetailResDTO));
    }

    @Override
    public void updatePermission(PermissionEditReqDTO reqDTO) {
        Long permissionId = reqDTO.getPermissionId();
        PermissionDetailResDTO oldPermissionDetailResDTO = this.detail(permissionId);
        SysPermissionEntity updateEntity = PermissionConvert.INSTANCE.editReqDTOConvert(reqDTO);
        updateEntity.setId(permissionId);
//        updateEntity.setUpdateBy(UserHolder.getUserId());
//        updateEntity.setUpdateByName(UserHolder.getUserName());
        updateEntity.setUpdateTime(LocalDateTime.now());
        this.updateById(updateEntity);

        // 保存操作日志
        PermissionDetailResDTO newPermissionDetailResDTO = this.detail(permissionId);
        operationLogService.save(OperationLogBO.getInstance()
                .biz(OperationLogTypeEnum.PERMISSION_UPDATE, permissionId, null)
                .content(reqDTO, oldPermissionDetailResDTO, newPermissionDetailResDTO));
    }

    @Override
    public void delete(Long permissionId) {
        SysPermissionEntity updateEntity = new SysPermissionEntity();
        updateEntity.setId(permissionId);
//        updateEntity.setUpdateBy(UserHolder.getUserId());
//        updateEntity.setUpdateByName(UserHolder.getUserName());
        updateEntity.setUpdateTime(LocalDateTime.now());
        this.updateById(updateEntity);
        this.removeById(permissionId);
    }
}
